// SPDX-License-Identifier: MIT OR Apache-2.0
//
// Copyright (c) 2019-2021 Andre Richter <andre.o.richter@gmail.com>

//! Console sanity tests - RX, TX and statistics.

#![feature(format_args_nl)]
#![no_main]
#![no_std]

use libkernel::{bsp, console, cpu, exception, print};

#[no_mangle]
unsafe fn kernel_init() -> ! {
    use bsp::console::console;
    use console::interface::*;

    exception::handling_init();
    bsp::console::qemu_bring_up_console();

    // Handshake
    assert_eq!(console().read_char(), 'A');
    assert_eq!(console().read_char(), 'B');
    assert_eq!(console().read_char(), 'C');
    print!("OK1234");

    // 6
    print!("{}", console().chars_written());

    // 3
    print!("{}", console().chars_read());

    // The QEMU process running this test will be closed by the I/O test harness.
    // cpu::wait_forever();

    // For some reason, in this test, rustc or the linker produces an empty binary when
    // wait_forever() is used. Calling qemu_exit_success() fixes this behavior. So for the time
    // being, the following lines are just a workaround to fix this compiler/linker weirdness.
    use libkernel::time::interface::TimeManager;
    libkernel::time::time_manager().spin_for(core::time::Duration::from_secs(3600));
    cpu::qemu_exit_success()
}
